package net.dlz.example.controller;

import net.dlz.example.domain.District;
import net.dlz.example.domain.Door;
import net.dlz.example.domain.Maps;

import net.dlz.example.domain.Street;
import net.dlz.example.service.DistrictService;
import net.dlz.example.service.DoorService;
import net.dlz.example.service.StreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;


import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/doors")
public class DoorController {
    @Autowired
    private DoorService doorService;


    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String doorPage(Model model) {
        List<Door> doorList = doorService.findAllDoor();
        model.addAttribute("doors", doorList);

        return "door_list1";
    }


    /**
     * get请求两个方法另一个是：创建新的门牌号
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addDoor(Model model) {
        List<District> districtList = districtService.listdistrict();
        model.addAttribute("districtList", districtList);//获取全部行政区信息
        model.addAttribute("door", new Door());
        return "door_add";
    }

    /**
     * 通过id查找到对应的信息并在add页面修改数据
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit")
    public String editDoorPage(@RequestParam("id") String id, Model model) {
        List<District> districtList = districtService.listdistrict();
        List<Street> streetList = streetService.listStreet();
        model.addAttribute("districtList", districtList);//获取全部行政区信息
        model.addAttribute("streetList", streetList);//获取全部街道信息
        Door door = doorService.findDoorByPrimaryKey(id);
        model.addAttribute("door", door);
        return "door_add";
    }

    /**
     * post请求将修改好的信息存入door表中并重定向到搜索界面
     *
     * @param door
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String saveDoor(Door door) {
        int update = doorService.saveDoor(door);
        return "redirect:/doors/search";
    }

    /**
     * 根据ID查找到该数据并删除
     *
     * @param id
     * @return
     */


    @DeleteMapping("delete")
    @ResponseBody
    public Boolean deleteDoor(@RequestParam("id") String id) {
        int deleted = doorService.deleteDoorByPrimsryKey(id);
        return deleted > 0;

    }

    /**
     * 从数据库读取经纬度数据，并将其转为json格式传输到前台
     *
     * @param request
     * @param hrequest
     * @param response
     * @return
     */
    @RequestMapping(value = "/getJson", method = RequestMethod.GET)
    @ResponseBody
    public List<Maps> getJson(ServletRequest request, HttpServletRequest hrequest, HttpServletResponse response) {


        List<Maps> listMaps = new ArrayList<Maps>();
        try {

            List<Door> list = doorService.getByLatitudeandLongitude();
            for (int i = 0; i < list.size(); i++) {
                Maps maps = new Maps();
                // int a = i + 1;
                maps.setIcon("http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png");
                maps.getPosition()[0] = list.get(i).getLng();
                maps.getPosition()[1] = list.get(i).getLat();
                listMaps.add(maps);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return listMaps;
    }


    @Autowired
    DistrictService districtService;
    @Autowired
    StreetService streetService;


    /**
     * 获取全部行政区
     * @param map
     * @return
     */
    // @RequestMapping(value = "/add")
//    public String getAllDistrict(Model model){
//        model.addAttribute("districtList",districtService.selectAllDistrict());
//        return "door_add";
//    }

    /**
     * 根据行政区获取街道并以json格式发送到前台
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getStreetByDistrictId")
    @ResponseBody
    public List<Street> getStreetByDistrictId(String id) {
        List<Street> streetList = streetService.getStreetByDistrictId(id);

        if (streetList != null) {

            return streetList;

        } else {
            return null;
        }
    }

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String districtStreet(Model model) {
        model.addAttribute("districtList", districtService.selectAllDistrict());
        return "statistics";
    }

    /**
     * 类别查询门牌
     * @param
     * @return
     */

    @RequestMapping("/toselectdoorByType")
    public String selectType(){
        return "statistics2";
    }
    @RequestMapping(value = "/selectdoorByType",method = RequestMethod.POST)

    public String  selectdoorByType(Integer type,Model model){
        List<Door> doorList = doorService.selectdoorByType(type);
        model.addAttribute("doorlist2",doorList);
        return "statistics2";


    }
    /**
     * 根据街道获取门牌
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getDoorByStreetId")
    @ResponseBody
    public List<Door> getDoorByStreetId(String id) {
        List<Door> doorList = doorService.getDoorByStreetId(id);
        if (doorList != null) {
            return doorList;
        } else {
            return null;
        }
    }

    @RequestMapping("streetDoorData")
    @ResponseBody
    public Object streetDoorData(String streetName, Model model) {
        List<Door> doors = doorService.streetDoor(streetName);
        model.addAttribute("doors", doors);
        return doors;
    }

}
